Counting Semaphore

There are scenarios in which more than one process needs to execute in the critical section simultaneously. However, a counting semaphore can be used when we need to have more than one process in the critical section at the same time. The programming code for semaphore implementation is shown below, which includes the structure of the semaphore and the logic for entry and exit in the critical section.

Warning

If you input a semaphore value less than or equal to 0, it will result in a deadlock and will show a warning.

Example to input the value

Here it is shown how the value of semaphore will be submitted.

Message of Acceptance

If you submit a semaphore value greater than zero, it will be accepted, and the visualization will start.

Buttons overview

Home : In the navbar, the Home button jumps to the main page.
Dropdown : Allows you to jump to any algorithm.
Info panel : Displays the value of each required queue and semaphore.
Add Process: Clicking this button adds a new process.
P0 : Clicking this button moves the P0 process forward according to the algorithm.
save PDF : Saves all the statuses of each stage of the algorithm.

Tool tips

Tooltips provide information about the hovered element. Elements with tooltips include:
semaphore, suspended queue, critical section, completed queue, Added, Entry, CS, Exit

To CS

By clicking the button, the process will move to the Critical Section if it is not occupied or if the semaphore is greater than zero (0).

Add Processes

By clicking "Add Processes," new processes will be added along with a button and process icon.

To Entry Section

Processes will move to the suspended queue if the critical section is occupied.
Example: P2 will move to the critical section and P3 to the suspended queue as the semaphore value becomes less than zero.

Warning

If a process is in the critical section and another process tries to interrupt, a warning will be displayed.

Completetion pop-up

A pop-up will appear if any process is completed.

Info Panel

Here, live value changes (in queues or variables) will be shown, which can be learned with each command given by the user to a button.

Full completion

It's a gratitude message displayed to indicate that all processes are completed.

Save PDF

By clicking "Save PDF," a PDF will be generated by collecting all text from the text area.

Analysis of Counting Semaphore approach

1.  In this mechanism, entry and exit in the critical section are based on the counting semaphore value. The counting semaphore value at any time indicates the maximum number of processes that can enter the critical section simultaneously.
2.  A process that wants to enter the critical section first decreases the semaphore value by 1 and then checks whether it becomes negative. If it becomes negative, then the process is added to the list of blocked processes (i.e., q); otherwise, it enters the critical section.
3.  When a process exits from the critical section, it increases the counting semaphore by 1 and then checks whether it is negative or zero. If it is negative, this means at least one process is waiting in a blocked state; hence, to ensure bounded waiting, the first process among blocked processes will wake up and enter the critical section.
4.  The processes in the blocked list will wake up in order of their sleep. If the counting semaphore value is negative, it indicates how many processes are blocked; if positive, it shows how many slots are available in the critical section.
5.  To implement mutual exclusion, initialize the counting semaphore with 1.
6.  This ensures that only one process can be present in the critical section at any given time.

abc